<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="员工姓名" prop="changerNickName">
                <el-input
                    v-model="queryParams.changerNickName"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="工号" prop="staffNum">
                <el-input
                    v-model="queryParams.staffNum"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['hrm:postChange:add']"
                    >
                        新增
                    </el-button>
                </el-col>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="postChangeList"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"/> -->
                <el-table-column
                    type="index"
                    label="序号"
                    width="55"
                    align="center"
                />
                <!-- <el-table-column label="单据编码" align="center" prop="billCode"/> -->
                <el-table-column
                    label="姓名（工号）"
                    align="center"
                    prop="changerNickName"
                >
                    <template slot-scope="scope">
                        <span>{{
                            scope.row.changerNickName +
                            "(" +
                            scope.row.staffNum +
                            ")"
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="性别" align="center" prop="sex">
                    <template slot-scope="scope">
                        <dict-tag
                            :options="dict.type.sys_user_sex"
                            :value="scope.row.sex"
                        />
                    </template>
                </el-table-column>
                <el-table-column
                    label="调前部门"
                    align="center"
                    prop="beforeDeptId"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseSelectTree(scope.row.beforeDeptId)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="调前岗位"
                    align="center"
                    prop="beforePostId"
                >
                    <template slot-scope="scope">
                        <span>{{ parsePostName(scope.row.beforePostId) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="调后部门"
                    align="center"
                    prop="afterDeptId"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseSelectTree(scope.row.afterDeptId)
                        }}</span>
                    </template></el-table-column
                >
                <el-table-column
                    label="调后岗位"
                    align="center"
                    prop="afterPostId"
                >
                    <template slot-scope="scope">
                        <span>{{ parsePostName(scope.row.afterPostId) }}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="调整原因" align="center" prop="reason"/> -->
                <el-table-column
                    label="调整时间"
                    align="center"
                    prop="submitTime"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.submitTime, "{y}-{m}-{d}") ||
                            "—"
                        }}</span>
                    </template>
                </el-table-column>
                <!--createTime -->
                <el-table-column
                    label="创建时间"
                    align="center"
                    prop="createTime"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.createTime, "{y}-{m}-{d}") ||
                            "—"
                        }}</span>
                    </template>
                </el-table-column>

                <!-- <el-table-column label="流程状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.oa_flow_billstatus" :value="scope.row.status"/>
                    </template>
                </el-table-column> -->
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            @click="handleView(scope.row)"
                            >查看</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            >编辑</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 10"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <FlowInfo
            :businessId="businessId"
            :billType="billType"
            v-if="showFlowInfo"
            :showStatus="showFlowInfo"
            @closeFlowInfoDialog="closeFlowInfoDialog"
        ></FlowInfo>
    </div>
</template>

<script>
import {
    listPostChange,
    getPostChange,
    delPostChange,
    addPostChange,
    updatePostChange,
} from "@/api/hrm/postChange";
import FlowInfo from "@/views/workflow/flow/flowInfo";
import { cancleFlow, submitFlow } from "@/api/workflow/processInstance";
import { treeselect } from "@/api/system/dept";
import { listPostAll } from "@/api/system/post";
export default {
    name: "PostChange",
    dicts: ["oa_flow_billstatus", "sys_user_sex"],
    components: { FlowInfo },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 岗位调整表格数据
            postChangeList: [],
            postList: [],
            // 查询参数
            queryParams: {
                staffNum:null, 
                pageNum: 1,
                pageSize: 10,
                orderByColumn:
                    this.$store.getters.allDefectObj["HR-WEB-058"] == 0
                        ? "createTime"
                        : "",

                billType: null,
                billCode: null,
                flowKey: null,
                flowInsId: null,
                submitTime: null,
                status: null,
                userId: null,
                deptId: null,
                tenantId: null,
                changerUserId: null,
                changerNickName: null,
                beforePostId: null,
                beforePostName: null,
                afterPostId: null,
                afterPostName: null,
                afterDeptId: null,
                reason: null,
            },
            // 表单校验
            billType: "",
            showFlowInfo: false,
            businessId: null,
            deptOptions: [],

            deptOptionsArray: [],
        };
    },
    created() {
        const billType = this.$route.query && this.$route.query.billType;
        console.log("单据类型：" + billType);
        if (billType) {
            this.queryParams.billType = billType;
            this.billType = billType;
        }
        this.getTreeselect();
        this.getList();

        this.getPostData();
    },
    methods: {
        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
                console.log(this.deptOptions, "this.deptOptions");

                this.deptOptionsArray = this.flattenArray(this.deptOptions);
                console.log(this.deptOptionsArray, " this.deptOptionsArray ");
            });
        },
        flattenArray(arr) {
            let result = [];
            for (let i = 0; i < arr.length; i++) {
                let item = arr[i];
                result.push(item);
                if (item.children) {
                    result = result.concat(this.flattenArray(item.children));
                }
            }
            return result;
        },

        parseSelectTree(id) {
            let tempObj = this.deptOptionsArray.find((item) => item.id == id);
            if (tempObj) {
                return tempObj.label;
            }
            return "—";
        },
        /** 查询岗位调整列表 */
        getList() {
            this.loading = true;
            listPostChange(this.queryParams).then((response) => {
                this.postChangeList = response.rows;
                console.log(this.postChangeList);
                this.total = response.total;
                this.loading = false;
            });
        },
        getPostData() {
            listPostAll().then((res) => {
                this.postList = res.data;
            });
        },
        parsePostName(ids) {
            let nameArr = [];
            let selIds = ids.split(",");
            this.postList.forEach((item) => {
                selIds.forEach((sel) => {
                    if (item.postId == sel) {
                        nameArr.push(item.postName);
                    }
                });
            });
            return nameArr.join(",");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id;
            this.$router.push({ path: "/hrm/postChange/form/" + row.id });
        },
        /** 查看单据详情 */
        handleView(row) {
            this.$router.push({
                path: "/hrm/postChange/form/" + row.id,
                query: { readonly: "1" },
            });
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$router.push({
                path: "/hrm/postChange/form/add/" + this.billType,
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否确认删除此条岗位调整的数据项？")
                .then(function () {
                    return delPostChange(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/postChange/export",
                {
                    ...this.queryParams,
                },
                `postChange_${new Date().getTime()}.xlsx`
            );
        },

        /** 提交审批 ***/
        doSubmitFlow(row) {
            let data = {
                businessId: row.id,
                billType: row.billType,
            };
            this.$modal
                .confirm("确认要提交该单据进行审批吗？")
                .then(() => {
                    return submitFlow(data);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("提交成功");
                })
                .catch(() => {});
        },
        /** 取回审批 ***/
        doCancleFlow(row) {
            let data = {
                businessId: row.id,
                billType: row.billType,
            };
            this.$modal.confirm("确认要取回该协议审批吗？").then(() => {
                cancleFlow(data).then((res) => {
                    if (200 == res.code) {
                        this.getList();
                        this.$modal.msgSuccess("取回成功");
                    }
                });
            });
        },
        /** 流程信息 ***/
        flowDetail(row) {
            this.businessId = row.id + "";
            this.showFlowInfo = true;
        },
        //关闭流程信息窗口
        closeFlowInfoDialog() {
            this.showFlowInfo = false;
        },
    },
};
</script>
